import Vue from 'vue'
import md5 from 'js-md5'
import store from '../../store'
import { ConfirmPlugin, ToastPlugin } from 'vux'
import { delCookie } from './cookie'
Vue.prototype.md5 = md5
Vue.use(ConfirmPlugin)
Vue.use(ToastPlugin)

/**
 * 接口调用token
 *
 */
export function token() {
    let time = JSON.stringify(new Date().getTime())
    let token = this.md5(time + '4baea6bc1bd66a7e00aa778d4aca6720')
    return [time, token]
}

/**
 * toast弹出框 text为弹出内容，time为弹出时间，position为弹出位置
 *
 */
export function toast(text, time, position) {
    this.$vux.toast.show({
        type: 'text',
        isShowMask: false,
        text: text,
        time: time,
        position : position || 'middle'
    });
    let _this = this
    setTimeout(function(){
        _this.$vux.toast.hide()
    }, time)
}

/**
 * 关闭toast弹出框
 *
 */
export function toastClose() {
    this.$vux.toast.hide()
}

/**
 * alert弹出框 content为弹出内容
 *
 */
export function alertVux(content, text) {
    this.$vux.confirm.show({
        // 组件除show外的属性
        content: content, 
        confirmText: text, 
        showCancelButton: false,
        hideOnBlur: true,
        onCancel () {
        },
        onConfirm () {
        }
    })
}

/**
 * 登录失效判断
 *
 */
export function logAgain(msg) {
    if(msg == '请先登录'){ 
        const _this = this // 需要注意 onCancel 和 onConfirm 的 this 指向
        this.$vux.confirm.show({
            // 组件除show外的属性
            content: '当前登录已失效，请重新登录',
            confirmText: '去登录',
            showCancelButton: false,
            hideOnBlur: true,
            onCancel () {
            },
            onConfirm () {
                _this.$router.push('/login')
            }
        })
        delCookie('userInfo') 
        sessionStorage.clear()
        // 更新用户信息
        store.commit('SET_USERINFO')
    } else {
        this.$toast(msg, 2000)
    }
} 

/**
 * 检查内容是否填写
 *
 */
export function checkWrite(arr, type) {
    let status = false
    for(let i = 0; i <arr.length; i++){
        if(!isDefine(arr[i].value)){
            if(type == 'toast'){
                this.$toast(arr[i].placeholder, 2000)
            }else{
                this.$alert(arr[i].placeholder)
            }
            status = true
            return status
        }
    }
    return status
}



const getClass = (name) => document.getElementsByClassName(name)

/**
 * 查询是否有值 有返回true
 *
 */
const isDefine = function (value) {
    if (value == null || value == '' || value == 'undefined' || value == undefined || value == 'null' || value == '(null)' || value == 'NULL' || typeof (value) === 'undefined') {
        return false
    }
    return true
}

/**
 * 查询是否空对象 是返回true
 *
 */
const isEmptyObject = function (value) {
    if(value.length > 0){
        for (let name in value){ 
            return false
        } 
    }
    return true
}

/**
 * 彩票下注/取消下注
 *
 */
const choose = function (playType0, title, playType1, content) {
    let id = playType0 + title[0] + playType1 + content
    let sel = document.getElementById(id)
    let name = sel.children[0].innerHTML // 选中name
    let odds = '' // 选中赔率
    if(isDefine(sel.children[2])){
        odds = sel.children[2].innerHTML
    }else{
        odds = sel.children[1].innerHTML
    }
    let {lotOptions} = store.state // 选中ID
    let {lotSelData} = store.state // 选中数据
    let contType = '' // 若内容为数字则添加‘-’显示
    if(isDefine(title[1])){
        contType = isNaN(Number(name)) ? '' : '-'
    }
    // 和数玩法name需单独截取
    if(title[0].indexOf('hs') > -1){
        name = title[1].substr(0, 2) + name
    }else{
        name = title[1] + contType + name
    }
    if(isDefine(playType0)){
        playType1 = playType0
    }else if(playType1 == ''){
        playType1 = title[0]
    }else if(playType1.indexOf('_' > -1)){ // 1大小单双
        playType1 = title[0] + playType1
    }
    let selObj = {
        'playType' : playType1,
        'content' : content,
        'odds' : Number(odds),
        'name' : name
    }
    // 判断是否为已选项
    if(lotOptions.indexOf(id) > -1){ // 已选则取消选中
        lotOptions = lotOptions.filter(n => n !== id)
        // 查找取消项数据下标并清除数据 
        for (var i = 0; i < lotSelData.length; i++) { 
            if (JSON.stringify(lotSelData[i]) == JSON.stringify(selObj)) lotSelData.splice(i, 1)
        } 
    }else{ // 未选则添加选项
        lotOptions.push(id) // 选中id
        lotSelData.push(selObj) // 选中数据
    }
    store.state.lotOptions = lotOptions // 选中id
    store.state.lotSelData = lotSelData // 选中数据
}

export { getClass, isDefine, isEmptyObject, choose }

